package com.alibaba.otter.node.etl.load.loader.mq.rabbit.strategy;

import org.apache.commons.collections.CollectionUtils;

import java.util.List;
import java.util.Map;

public class FailureAction {

    private Map<Class<? extends Throwable>, List<RescueAction>> rescueActionMap;

    private Map<Class<? extends Throwable>, EndAction> endActionMap;

    private EndAction defaultEndAction = new BlockUntil();

    List<RescueAction> getExceptionHandler(Class<? extends Throwable> ex) {
        List<RescueAction> results = rescueActionMap.get(ex);
        if (CollectionUtils.isEmpty(results)) {
            results = rescueActionMap.get(Exception.class);
        }
        return results;
    }

    EndAction getEndStrategy(Class<? extends Throwable> ex) {
        EndAction result = endActionMap.get(ex);
        result = result == null ? endActionMap.get(Exception.class) : result;
        result = result == null ? defaultEndAction : result;
        return result;
    }

    void setRescueActionMap(Map<Class<? extends Throwable>, List<RescueAction>> rescueActionMap) {
        this.rescueActionMap = rescueActionMap;
    }

    void setEndActionMap(Map<Class<? extends Throwable>, EndAction> endActionMap) {
        this.endActionMap = endActionMap;
    }
}
